/*
 * Copyright (c) 2021.版权所有高金磊
 */

package 领扣算法.AA中等题.魔塔游戏;

import java.util.Arrays;
import java.util.PriorityQueue;

public class Main {
    public int magicTower(int[] nums) {
        long middle=0,count=0,sum=0;
        PriorityQueue<Integer> data=new PriorityQueue<>((a,b)->(a-b));
        if (Arrays.stream(nums).sum()<0)
            return -1;
        for (int num : nums) {
            data.add(num);
            sum+=num;
            if (sum<0)
            {
                count++;
                middle+=data.peek();
                sum-=data.poll();
            }
        }
        return (int) count;
    }
}
